<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function choosesort(arr) {
        var minIndex;
        for (var i = 0; i < arr.length - 1; i++) {
          //找最小值的下标 将下标放在minIndex 中 然后当前第一位和arr[minIndex]互换
          minIndex = i;
          for (var n = i; n < arr.length; n++) {
            if (arr[minIndex] > arr[n]) {
              minIndex = n;
            }
          }
          //交换数据
          var tmp = arr[i];
          arr[i] = arr[minIndex];
          arr[minIndex] = tmp;
        }
        return arr;
      }
      var arr = [3, 5, 4, 1, 2];
      console.log(choosesort(arr));
    </script>
  </body>
</html>
